package com.hwz.service.impl;

import com.hwz.constant.StatusConstant;
import com.hwz.context.BaseContext;
import com.hwz.entity.AddressBook;
import com.hwz.mapper.AddressBookMapper;
import com.hwz.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Lenovo]
 * @version : [v1.0]
 * @createTime : [2024/3/19 21:23]
 */
@Service
@Slf4j
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Override
    public void insertAddressBook(AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBook.setIsDefault(StatusConstant.DISABLE);
        addressBookMapper.insert(addressBook);
    }

    @Override
    public List<AddressBook> getaddressBookList() {
        Long userId = BaseContext.getCurrentId();

        return addressBookMapper.getByUserId(userId);
    }

    @Override
    public AddressBook getAddressBookByDefault() {
        Long userId = BaseContext.getCurrentId();
        return addressBookMapper.getByUserIdAndDefault(userId);
    }

    @Override
    public void updateAddressBook(AddressBook addressBook) {
        addressBookMapper.updateById(addressBook);
    }

    @Override
    public void deleteAddressBook(Long id) {
        addressBookMapper.deleteById(id);
    }

    @Override
    public AddressBook getAddressBookById(Long id) {
        log.info("查询地址簿信息#{}",id);
        return addressBookMapper.getById(id);
    }

    @Override
    public void setDefault(AddressBook addressBook) {
        addressBook.setIsDefault(0);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.updateIsDefaultByUserId(addressBook);
        addressBook = addressBookMapper.getById(addressBook.getId());
        addressBook.setIsDefault(StatusConstant.ENABLE);
        addressBookMapper.updateById(addressBook);
    }
}
